package ap;

public class Ap {

    public static void main(String[] args) {
        char[] a = "Write once - run anywhere!".toCharArray();
        int current = 0;

        System.out.print("Source - ");
        showArr(a);
        reflexString(a, 0, a.length - 1);

        for (int i = 0; i < a.length; i++) {
            if (a[i] == ' ') {
                reflexString(a, current, i - 1);
                current = i + 1;
            }
        }
        System.out.print("Result - ");
        showArr(a);
    }

    private static void reflexString(char[] arr, int first, int last) {
        do {
            char c = arr[first];
            arr[first] = arr[last];
            arr[last] = c;
            first++;
            last--;
        } while (first <= last);
    }

    private static void showArr(char[] c) {
        for (char d : c) {
            System.out.print(d);
        }
        System.out.println();
    }
}
